<?php
namespace App\Http\Controllers\Usere;
use App\Http\Controllers\BaseApiController;
use Illuminate\Http\Request;
use App\Services\Usere\UsereEdit;
use Validator;
class UsereEditController extends BaseApiController
{
    private $userEdit;
    public function __construct(UsereEdit $usereEdit ){
        $this->userEdit = $usereEdit;
    }
    //查询一条数据
    public function find(Request $request){
        //接收id
        $id = $request->post('id');
        $data['id'] = $id;
        $rule = ['id'=>'required|integer'];
        $tips = ['id.required'=>'不能为空','id.integer'=>'必须为整形'];
        $val = $this->validator($data,$rule,$tips);
        if($val->fails()){
            $error = $val->errors();
            return $error;
        }
        $res  = json_encode($this->userEdit->find($id));

        return $this->result('2000',$res,'success');
    }
    //编辑资料
    public function edit(Request $request){
        //接收数据
        $data = $request->post();
        $rule = ['head_portrait'=>'required','name'=>'required','gender'=>'required','major'=>'required','education'=>'required','graduation_date'=>'required|integer'];
        $tips = ['head_portrait.required'=>'不能为空','name.required'=>'不能为空','gender.required'=>'不能为空','major.required'=>'不能为空','education.required'=>'不能为空','graduation_date.required'=>'不能为空','graduation_date.integer'=>'为整形'];
        $val = $this->validator($data,$rule,$tips);
        if($val->fails()){
            $error = $val->errors();
            return $error;
        }
        if(!$this->userEdit->edit($data)){
            return $this->result('1004','修改失败','error');
        }
        $res = $this->userEdit->edit($data);
        return $this->result('2000',$res,'success');
    }
    //修改密码
    public function modifyPwd(Request $request){
        $newPwd = $request->post('pwd');
        $data['pwd'] = $newPwd;
        $rule = ['pwd'=>'required|max:20'];
        $tips = ['pwd.required'=>'密码不能为空','pwd.max'=>'密码最大20位'];
        $val = $this->validator($data,$rule,$tips);
        if($val->fails()){
            $error = $val->errors();
            return $error;
        }
        //获取用户id
        $user_id = 1;
        $arr['password'] = md5($newPwd);
        $res = $this->userEdit->modifyPwd($user_id,$arr);
        return $this->result('2000',$res,'success');
    }
}